<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use \App\Post;
use \App\Comment;
use \App\Zan;

class PostController extends Controller
{
    // 列表
    public function index() {
        $posts = Post::orderBy('created_at', 'desc')->withCount(['comments','zans'])->paginate(6);
        $topics = [];
        return view("post/index", compact('posts', 'topics'));  // compact 建立一个数组 变量名为 key
    }

    // 详情页
    public function show(Post $post) {
        $post->load('comments');    // 渲染模板之前加载
        return view("post/show", compact('post'));
    }

    // 创建文章
    public function create() {
        return view("post/create");
    }

    // 创建逻辑 
    public function store() {
        /* 传统的保存方法
        $post = new Post();
        $post->title = request('title');
        $post->content = request('content');
        $post->save();
        */
        
        // 验证
        $this->validate(request(), [
            'title' => 'required | string | max:100 | min:5',
            'content' => 'required | string | min:10',
        ]);

        // 逻辑
        $user_id = \Auth::id();
        $params = array_merge(request(['title', 'content']), compact('user_id'));
        Post::create($params);

        // 渲染
        return redirect("/posts");
    }

    // 编辑页面 
    public function edit(Post $post) {
        return view("post/edit", compact('post'));
    }

    // 编辑逻辑 
    public function update(Post $post) {
        // 验证
        $this->validate(request(), [
            'title' => 'required | string | max:100 | min:5',
            'content' => 'required | string | min:10',
        ]);

        // 授权验证, 即使被仿造表单， 也会被拦截
        $this->authorize('update', $post);

        // 逻辑
        $post->title = request('title');
        $post->content = request('content');
        $post->save();

        // 渲染
        return redirect("/posts/{$post->id}");
    }

    // 删除逻辑
    public function delete(Post $post) {

        // TODO: 用户权限验证

        // 授权验证
        $this->authorize('delete', $post);

        $post->delete();

        return redirect("/posts");
    }

    // 上传图片
    public function imageUpload(Request $request) {
        // 从请求拿出 'wangEditorH5File' 储存到 public 的空间 并且重命名, 返回的文件存在 storage/
        $path = $request->file('wangEditorH5File')->storePublicly(md5(time()));
        return asset('storage/'. $path); 
    }

    // 提交评论
    public function comment(Post $post){
        // 验证
        $this->validate(request(), [
            'content' => 'required | min:3',
        ]);
        // 逻辑  创建comment对象，为对象赋值， 然后通过 post 与 comment的关联方法 进行保存
        $comment = new Comment();
        $comment->user_id = \Auth::id();
        $comment->content = request('content');
        $post->comments()->save($comment);

        // 渲染
        return back();
    }

    // 赞
    public function zan(Post $post){
        $param = [
            'user_id' => \Auth::id(),
            'post_id' => $post->id,
        ];
        Zan::firstOrCreate($param);
        return back();
    }

    // 取消赞
    public function unzan(Post $post) {
        // 查找是否存在关联关系，若存在，则删除， 实现取消赞
        $post->zan(\Auth::id())->delete();
        return back();
    }
}
